/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.frontend.blog.client.blog.blogentry.comment;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.blog.client.blog.blogentry.comment.events.in.ShowBlogEntryComment;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.i18n.client.I18N;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.entry.comment.BlogEntryCommentNewDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.entry.comment.BlogEntryCommentAsyncCallback;
import com.googlecode.hmvc4java.event.IEvent;
import com.googlecode.hmvc4java.registry.controller.AControllerModelViewRegistry;

/**
 * 
 * @author jens.meiss
 */
public class BlogEntryCommentController extends
		AControllerModelViewRegistry<BlogEntryCommentModel, BlogEntryCommentView> {
	
	public void init() {
		
		register(ShowBlogEntryComment.class);
		
	}

	public void terminate() {
	}

	public void handleEvent(IEvent event) {
		
		if (event.getClass() == ShowBlogEntryComment.class) {
			
			Panel panelParent = (((ShowBlogEntryComment) event).getPanelParent());
			
			getView().setPanelParent(panelParent);
			
			panelParent.add(getView().getWidget());
			
		}
		
	}

	public void initView() {
		
		Tree treeBlogEntryComment = new Tree();
		treeBlogEntryComment.setAnimationEnabled(true);
		
		treeBlogEntryComment.setTitle("Show labelComments");
		getView().setTreeBlogEntryComment(treeBlogEntryComment);
		
		Button buttonAddComment = new Button(I18N.constants.buttonAddBlogEntryComment());
		getView().setButtonAddComment(buttonAddComment);
		
		buttonAddComment.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				addComment();
			}
		});
		
		TreeItem treeItemButton = new TreeItem(buttonAddComment);
		
		treeBlogEntryComment.addItem(treeItemButton);
		
	}

	private void addComment() {
		
		getView().getTreeBlogEntryComment().remove(getView().getButtonAddComment());
		
		Label labelCommentAuthorName = new Label("Author Name");
		TextBox textBoxCommentAuthorName = new TextBox(); 
		getView().setTextBoxCommentAuthorName(textBoxCommentAuthorName);
		
		Label labelTextAreaComment = new Label("Your Blog-Comment");
		TextArea textAreaComment = new TextArea();
		getView().setTextAreaComment(textAreaComment);
		
		Button buttonCommitComment = new Button(I18N.constants.buttonAddBlogEntryComment());
		buttonCommitComment.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				
				commitComment();
				
			}
		});
		
		Button buttonCancel = new Button(I18N.constants.buttonCancel());
		buttonCancel.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				
				getView().getTreeBlogEntryComment().removeItem(getView().getTreeItemComment());
				getView().getTreeBlogEntryComment().add(getView().getButtonAddComment());
				
			}
		});
		
		FlexTable flexTable = new FlexTable();
		flexTable.setWidget(0, 0, labelCommentAuthorName);
		flexTable.setWidget(0, 1, textBoxCommentAuthorName);
		flexTable.setWidget(1, 0, labelTextAreaComment);
		flexTable.setWidget(1, 1, textAreaComment);
		flexTable.setWidget(2, 0, buttonCommitComment);
		
		FlexCellFormatter flexCellFormatter = flexTable.getFlexCellFormatter();
		flexCellFormatter.setColSpan(2, 0, 2);
		
		flexCellFormatter.setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		
		TreeItem treeItem = new TreeItem();
		getView().setTreeItemComment(treeItem);
		treeItem.setWidget(flexTable);
		
		getView().getTreeBlogEntryComment().addItem(treeItem);
		
		
	}
	
	private void commitComment() {
		
		BlogEntryCommentNewDTO blogEntryCommentNewDTO = new BlogEntryCommentNewDTO();
		blogEntryCommentNewDTO.setAuthor(getView().getTextBoxCommentAuthorName().getText());
		blogEntryCommentNewDTO.setText(getView().getTextAreaComment().getText());
		blogEntryCommentNewDTO.setIdBlogEntry(getModel().getBlogEntryIdDTO().getId());
		
		BlogEntryCommentAsyncCallback.addBlogEntryComment(blogEntryCommentNewDTO, this);
		
		
	}
	
	public void initModel() {
		// TODO Auto-generated method stub
		
	}

}
